package org.argeproje.resim.ui.model;

import org.argeproje.resim.proc.Processor;
import org.argeproje.resim.property.Property;
import org.argeproje.resim.property.PropertyManager;
import org.argeproje.resim.ui.graphics.IconFigure;
import org.eclipse.swt.graphics.Image;


public abstract class ComponentItem extends Shape{
	private static final long serialVersionUID = 1;
	private static final Image COMPONENT_ICON = createImage("icons/rectangle16.gif");
	
	protected String _name;
	protected String _description;
	//protected Class cvChildClass;
	private transient PropertyManager _propertyManager;
	protected transient Processor _processor;
	protected transient IconFigure _iconFigure;
	public void setIconFigure(IconFigure iconFigure){
		_iconFigure = iconFigure;
	}
	public IconFigure getIconFigure(){
		return _iconFigure;
	}

	public ComponentItem()
	{
		super();		
	}
	
	public Processor getProcessor()
	{
		updateProc();
		return _processor;
	}
		
	private PropertyManager getPropertyManager()
	{		
		if(_propertyManager==null)
		{
			_propertyManager = new PropertyManager();
		}
		return _propertyManager;
	}
	
	/*
	public void setClass(Class clsID){
		cvChildClass = clsID;
	}*/
	
	public abstract Class getComponentClass();
	
	protected void registerProperty(Property property)
	{
       descriptors.add(property.getDescriptor());
       getPropertyManager().addProperty(property);
	}	
	/*protected void registerFileSelectProperty(Object instance,String displayName,String propertyId,String getterName,String setterName)
	{
       FileSelectPropertyDescriptor lPropDesc = new FileSelectPropertyDescriptor(propertyId,displayName);       
       descriptors.add(lPropDesc); 
       
       Method mGetter=null;
       Method mSetter=null;
       
		try {
			mGetter = getComponentClass().getMethod(getterName);		
			mSetter = getComponentClass().getMethod(setterName,Object.class);
			getPropertyManager().addProperty(new Property(instance,-1,propertyId,mGetter,mSetter));
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
	}
	*/
	/*protected void registerMatrixProperty(Object instance,String displayName,KernelDA kernel,String propertyId,String getterName,String setterName)
	{
       MatrixPropertyDescriptor lPropDesc = new MatrixPropertyDescriptor(propertyId,displayName,kernel);       
       descriptors.add(lPropDesc); 
       
       Method mGetter=null;
       Method mSetter=null;
       
		try {
			mGetter = getComponentClass().getMethod(getterName);		
			mSetter = getComponentClass().getMethod(setterName,Object.class);
			getPropertyManager().addProperty(new Property(instance,-1,propertyId,mGetter,mSetter));
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
	}*/
	
	
		
	public ComponentItem(String Name,String Description)
	{
	  super();
	  _name = Name;
	  _description = Description;
	}
	
	public Object getPropertyValue(Object propertyId) {
		if(_propertyManager.isRegisteredProperty(propertyId.toString()))
		{
			return _propertyManager.getProperty(propertyId.toString()).getValue();
		}
		return super.getPropertyValue(propertyId);
	}
	
	public void setPropertyValue(Object propertyId, Object value) {
		if(_propertyManager.isRegisteredProperty(propertyId.toString()))
		{
			_propertyManager.getProperty(propertyId.toString()).setValue(value);
		}
		super.setPropertyValue(propertyId,value);
	}
	
	public String GetName(){ return _name; }
	public String GetDescription(){ return _description; }

	public Image getIcon() {
		return COMPONENT_ICON;
	}	
	
	public String toString() {
		return "Component " + hashCode();
	}
	
	/*
	protected void readObject(ObjectInputStream in) throws IOException,
	ClassNotFoundException {
		super.readObject(in);
		cvPropMgr = new PropertyManager();		
	}*/
	public abstract void initProc();
	public abstract void updateProc();
}
